export default {
  props: {
    renderOptions: Object,
    params: Object,
    toolbarProps: { // 详见antd button参数
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      visible: true,
      disabled: false
    }
  },
  watch: {
    'toolbarProps.visible': {
      async handler() {
        if (this.toolbarProps?.visible && typeof this.toolbarProps.visible === 'function') {
          const { button, $grid } = this.params
          this.visible = await this.toolbarProps.visible({
            button,
            $grid
          })
          return
        }
        this.visible = this.toolbarProps?.visible !== false
      },
      deep: true
    },
    'toolbarProps.disabled': {
      async handler(val) {
        if (val === '' || val === undefined || val === null) {
          return
        }
        if (this.toolbarProps?.disabled && typeof this.toolbarProps.disabled === 'function') {
          const { button, $grid } = this.params
          this.disabled = await this.toolbarProps.disabled({
            button,
            $grid
          })
          return
        }
        this.disabled = this.toolbarProps?.disabled
      },
      deep: true,
      immediate: true
    },
    'renderOptions.disabled': { // disabled属性在renderOptions指定。后续在正式版会废弃，统一调整为props内指定
      async handler(val) {
        if (val === '' || val === undefined || val === null) {
          return
        }
        this.disabled = this.renderOptions?.disabled
      },
      deep: true,
      immediate: true
    }
  },
  // computed: {
  //   disabled() {
  //     if (this.toolbarProps?.disabled && typeof this.toolbarProps.disabled === 'function') {
  //       const { button, $grid } = this.params
  //       return this.toolbarProps.disabled({
  //         button,
  //         $grid
  //       })
  //     }
  //     return this.renderOptions.disabled || this.toolbarProps?.disabled
  //   }
  // },
  methods: {
    handleClick() {
      const { events = {}} = this.renderOptions
      const { button, $grid } = this.params
      events.click(button, $grid)
    }
  }
}
